//
//  OnlineSubMenuVC.m
//  GC_OC
//
//  Created by lbh on 2022/4/5.
//

#import "OnlineSubMenuVC.h"
#import "OnlineSubHeadView.h"
#import "OnLineSubMenuLIstVC.h"
#import "OnLineTeacherDetailVC.h"
#import "EmptyView.h"

@interface OnlineSubMenuVC ()
@property (nonatomic,strong)OnlineSubHeadView *subHeadView;
@property (nonatomic, strong)ObjectAFNetWorking* objectAFNet;
@property (nonatomic,strong)UIView *navBarView;
@property (nonatomic,strong)NSDictionary *currenClassDict;
@property(nonatomic,assign)CGRect rect;
@property(nonatomic,assign)CGRect sRect;
@property(nonatomic,assign)CGRect ssRect;
@property (nonatomic)CGFloat pageViewH;//
@property (nonatomic,strong)EmptyView *emptyView;
@end

@implementation OnlineSubMenuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //34律法
    self.pageViewH = 200;
    [self creatHeadView];
    [self getTearchListData];
    [self setNeedsStatusBarAppearanceUpdate];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor=AppMainColor;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
//导航栏设置
-(void)creatHeadView{
    
    self.objectAFNet=[[ObjectAFNetWorking alloc]init];
    
    self.view.backgroundColor=UIColor.whiteColor;
    UIView *navBarView=[[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,topHeight)];
    navBarView.backgroundColor = AppMainColor;
    [self.view addSubview:navBarView];
    self.navBarView = navBarView;
    
    CGFloat titleHeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)] andWidth:wid(100)];
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(wid(95),StatusBarHeight+(topHeight-StatusBarHeight-titleHeight)/2.0,SCREEN_WIDTH-wid(65)*2,titleHeight)];
    titleLabel.textColor=[UIColor whiteColor];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)]];
    titleLabel.textAlignment=NSTextAlignmentLeft;
    titleLabel.text=self.vcTitle;
    [navBarView addSubview:titleLabel];
    
    UIButton* leftbtn=[UIButton new];
    leftbtn.frame=CGRectMake(wid(20),StatusBarHeight+(topHeight-StatusBarHeight-wid(40))/2.0,wid(40),wid(40));
    [leftbtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    leftbtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [leftbtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [navBarView addSubview:leftbtn];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLineShow) name:@"changeLineShow" object:nil];
    
    self.emptyView.bEmptyLabel.text = @"暂无数据";
    self.emptyView.bEmptyImageView.image = [UIImage imageNamed:@"无数据"];
    [self.view addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
    }];
}
//分页设置(有头部数据)
- (void)createPageView{
    NSArray *titleArray = [self getTabHeadViewAllTitle];
    NSMutableArray *data = [[NSMutableArray alloc]init];
    for (NSString *str in titleArray) {
        NSDictionary *dict = @{WMZPageKeyName:str,WMZPageKeyTitleMarginY:@(-4)};
        [data addObject:dict];
    }
    if (titleArray.count == 0) {
        OnLineSubMenuLIstVC *vc = OnLineSubMenuLIstVC.new;
        vc.page = 0;
        vc.ID = self.ID;
        vc.view.frame = CGRectMake(0, topHeight, SCREEN_WIDTH, SCREEN_HEIGHT - topHeight);
        [self addChildViewController:vc];
        [self.view addSubview:vc.view];
        return;
    }
    //标题数组
    WMZPageParam *param = PageParam();
    param.wMenuImagePositionSet(PageBtnPositionLeft)
        .wMenuTitleColorSet(UIColor.blackColor)
        .wMenuTitleSelectColorSet(UIColor.blackColor)
        .wMenuTitleUIFontSet([UIFont systemFontOfSize:18])
        .wMenuTitleSelectUIFontSet([UIFont boldSystemFontOfSize:18])
        .wTitleArrSet(data)
        .wMenuIndicatorColorSet(UIColor.clearColor)
        .wMenuIndicatorWidthSet(30)
        .wMenuIndicatorRadioSet(2)
        .wMenuHeightSet(50)
        .wMenuIndicatorYSet(3)
        .wMenuBottomMarginYSet(-0)
        .wMenuSelectTitleBackgroundSet(UIColor.whiteColor)
    //控制器数组
        .wViewControllerSet(^UIViewController *(NSInteger index) {
            OnLineSubMenuLIstVC *vc = OnLineSubMenuLIstVC.new;
            vc.page = index;
            vc.classArray = [self getClassContentArray:index];
            vc.ID = self.ID;
            return (UIViewController*)vc;
        })
//        .wMenuTitleWidthSet(PageVCWidth/titleArray.count)
//        .wMenuTitleWidthSet(PageVCWidth/titleArray.count)
        .wMenuPositionSet(PageMenuPositionLeft)
        .wMenuAnimalSet(PageTitleMenuPDD)
        .wMenuBgColorSet(AppMainGrayColor)
    //悬浮开启
        .wTopSuspensionSet(YES)
    
    //顶部可下拉
        .wBouncesSet(NO)
    //头视图y坐标从导航栏开始
        .wFromNaviSet(YES)
    //导航栏透明度变化
        .wNaviAlphaSet(NO)
    //下划线
        .wInsertMenuLineSet(^(UIView *bgView) {
            //修改内置线
            CGRect rect = bgView.frame;
            if (titleArray.count == 0) {
                rect.size.height = 0;
            }
            else{
                rect.size.height = 0;
            }
            rect.origin.y -= 0;
            bgView.frame = rect;
            bgView.backgroundColor = RBG(244, 244, 244);
        })
    //头部
        .wMenuHeadViewSet(^UIView *{
                return self.subHeadView;
            })
    //可以控制标题是否悬浮
        .wCustomDataViewHeightSet(^CGFloat(CGFloat nowY) {
            return nowY + 50;
        });
    self.param = param;
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-5);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    self.upSc.backgroundColor = RBG(246,247,249);
    self.pageView.backgroundColor=RBG(246,247,249);
}
//分页设置(没有头部数据)
- (void)createNOHeadViewPageView{
    NSArray *titleArray = [self getTabHeadViewAllTitle];
    NSMutableArray *data = [[NSMutableArray alloc]init];
    for (NSString *str in titleArray) {
        NSDictionary *dict = @{WMZPageKeyName:str,WMZPageKeyTitleMarginY:@(-4)};
        [data addObject:dict];
    }
    if (titleArray.count == 0) {
        OnLineSubMenuLIstVC *vc = OnLineSubMenuLIstVC.new;
        vc.page = 0;
        vc.ID = self.ID;
        vc.view.frame = CGRectMake(0, topHeight, SCREEN_WIDTH, SCREEN_HEIGHT - topHeight);
        [self addChildViewController:vc];
        [self.view addSubview:vc.view];
        return;
    }
    //标题数组
    WMZPageParam *param = PageParam();
    param.wMenuImagePositionSet(PageBtnPositionLeft)
        .wMenuTitleColorSet(UIColor.blackColor)
        .wMenuTitleSelectColorSet(UIColor.blueColor)
        .wMenuTitleUIFontSet([UIFont boldSystemFontOfSize:18])
        .wMenuTitleSelectUIFontSet([UIFont boldSystemFontOfSize:18])
        .wTitleArrSet(data)
        .wMenuIndicatorColorSet(AppMainColor)
        .wMenuIndicatorWidthSet(30)
        .wMenuIndicatorRadioSet(2)
        .wMenuHeightSet(50)
        .wMenuIndicatorYSet(3)
        .wMenuBottomMarginYSet(-10)
    //控制器数组
        .wViewControllerSet(^UIViewController *(NSInteger index) {
            OnLineSubMenuLIstVC *vc = OnLineSubMenuLIstVC.new;
            vc.page = index;
            vc.classArray = [self getClassContentArray:index];
            vc.ID = self.ID;
            return (UIViewController*)vc;
        })
        .wMenuTitleWidthSet(PageVCWidth/titleArray.count)
        .wMenuPositionSet(PageMenuPositionLeft)
        .wMenuAnimalSet(PageTitleMenuPDD)
    //悬浮开启
        .wTopSuspensionSet(YES)
    
    //顶部可下拉
        .wBouncesSet(NO)
    //头视图y坐标从导航栏开始
        .wFromNaviSet(YES)
    //导航栏透明度变化
        .wNaviAlphaSet(NO)
    //下划线
        .wInsertMenuLineSet(^(UIView *bgView) {
            //修改内置线
            CGRect rect = bgView.frame;
            if (titleArray.count == 0) {
                rect.size.height = 0;
            }
            else{
                rect.size.height = 1;
            }
            rect.origin.y -= 1;
            bgView.frame = rect;
            bgView.backgroundColor = RBG(244, 244, 244);
        })
    //头部
    //可以控制标题是否悬浮
        .wCustomDataViewHeightSet(^CGFloat(CGFloat nowY) {
            return nowY + 50;
        });
    self.param = param;
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-5);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    self.upSc.backgroundColor = RBG(246,247,249);
    self.pageView.backgroundColor=RBG(246,247,249);
}
//获取所有bottom字标题
- (NSArray *)getTabHeadViewAllTitle{
    NSMutableArray *titleArray = [[NSMutableArray alloc]init];
    id bottom = self.currenClassDict[@"data"][@"bottom"];
    if ([bottom isKindOfClass:[NSArray class]]) {
        NSArray *bottomArray = (NSArray *)bottom;
        bottomArray = [[bottomArray reverseObjectEnumerator] allObjects];
        for (NSDictionary *botdict in bottomArray) {
            NSString *nTitle = botdict[@"tile"];
            [titleArray addObject:nTitle];
        }
    }
    return titleArray;
}
///获取字典中content数组数据
- (NSArray *)getClassContentArray:(NSInteger)section{
    NSArray *classArray = [self getClassArrayData];
    NSDictionary *classDict = classArray[section];
    NSArray *content = classDict[@"content"];
    content = [[content reverseObjectEnumerator] allObjects];
    return content;
}
///组装所有的课程
- (NSArray *)getClassArrayData{
    NSMutableArray *DictArray = [[NSMutableArray alloc]init];
    id bottom = self.currenClassDict[@"data"][@"bottom"];
    if ([bottom isKindOfClass:[NSArray class]]) {
        NSArray *bottomArray = (NSArray *)bottom;
        for (NSDictionary *botdict in bottomArray) {
            [DictArray addObject:botdict];
        }
    }
    return DictArray;
}
-(void)leftBtnAction{
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)changeLineShow{
    NSArray *titleArray = [self getTabHeadViewAllTitle];
    if (titleArray.count == 0) {
        return;
    }
    self.param.wInsertMenuLineSet(^(UIView *bgView) {
        //修改内置线
        CGRect rect = bgView.frame;
        rect.size.height = 0;
        rect.origin.y -= 1;
        bgView.frame = rect;
        bgView.backgroundColor = RBG(244, 244, 244);
    });
    [self updatePageController];
}
#pragma mark --NetWork
// 根据课程ID获取教师列表
-(void)getTearchListData{
    [self.view showBusyHUD];
    NSString *url = [NSString stringWithFormat:@"%@/api/home/getCateData?id=%@",server_addr,self.ID];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/OnlineEducation_%@_getCateData_Json.json",self.ID]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData* dataString=[NSData dataWithContentsOfFile:filePath];
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        NSArray *teacher = dic[@"data"][@"teacher"];
        self.currenClassDict = dic;
        self.subHeadView.dataSource = teacher;
        if (teacher.count) {
            [self createPageView];
        }
        else{
            [self createNOHeadViewPageView];
        }
        //保存数据
        [self.view hideBusyHUD];
        [GC_OCTool saveFileAction:filePath];
    }else{
        [self.objectAFNet getUrlStr:url finish:^(id  _Nonnull dataString) {
            [self.view hideBusyHUD];
            NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
            NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/OnlineEducation_%@_getCateData_Json.json",self.ID]];
            if ([fileManager fileExistsAtPath:filePath]) {
                [fileManager removeItemAtPath:filePath error:nil];
            }
            [dataString writeToFile:filePath atomically:YES];
            NSArray *teacher = dic[@"data"][@"teacher"];
            self.currenClassDict = dic;
            self.subHeadView.dataSource = teacher;
            if (teacher.count) {
                [self createPageView];
            }
            else{
                [self createNOHeadViewPageView];
            }
        } andError:^(NSError * _Nonnull error) {
            [self.view hideBusyHUD];
        }];

    }
}

#pragma mark --lazy
- (OnlineSubHeadView *)subHeadView{
    if (!_subHeadView) {
        _subHeadView = [[OnlineSubHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 230)];
        _subHeadView.mediClassID = self.mediCalssID;
    }
    return _subHeadView;
}
//空视图
- (EmptyView *)emptyView {
    
    if (!_emptyView) {
        
        _emptyView = [[EmptyView alloc]init];
        _emptyView.backgroundColor = [UIColor clearColor];
        _emptyView.hidden = YES;
    }
    return _emptyView;
}
@end

